package com.yanqu.road.dao.impl.activity.tanghulu;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.tanghulu.TangHuLuMakeData;
import com.yanqu.road.entity.activity.tanghulu.TangHuLuUserData;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;

public class TangHuLuUserDataDaoImpl extends TempDataObjectDao<TangHuLuUserData> {


    public static String getTangHuLuUserDataSql = "select * from t_u_activity_tanghulu where activityId=? and userId=?";
    public TangHuLuUserData getTangHuLuUserData(int activityId, long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getTangHuLuUserDataSql);
            int i = 1;
            call.setInt(i++,activityId);
            call.setLong(i++,userId);
            rs = call.executeQuery();
            while (rs.next()) {
                return parse(rs);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    public static String insertTangHuLuUserDataSql = "insert into t_u_activity_tanghulu(activityId,userId,makeData,totalScore,sweetVal,tangHuLuNum,seniorMakeData) values (?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(TangHuLuUserData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        if(data.getCurGame() == null){
            para.put(i++, new DbParameter(Types.VARCHAR, ""));
        }else {
            para.put(i++, new DbParameter(Types.VARCHAR, JSONObject.toJSONString(data.getCurGame())));
        }
        para.put(i++, new DbParameter(Types.BIGINT, data.getTotalScore()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getSweetVal()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getTangHuLuNum()));
        if(data.getSeniorMakeData() == null){
            para.put(i++, new DbParameter(Types.VARCHAR, ""));
        }else {
            para.put(i++, new DbParameter(Types.VARCHAR, JSONObject.toJSONString(data.getSeniorMakeData())));
        }
        if (getDb().executeNoneQuery(insertTangHuLuUserDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String updateTangHuLuUserDataSql = "update t_u_activity_tanghulu set makeData=?,totalScore=?,sweetVal=?,tangHuLuNum=?,seniorMakeData=? where activityId=? and userId=?";
    @Override
    protected boolean updateData(TangHuLuUserData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        if(data.getCurGame() == null){
            para.put(i++, new DbParameter(Types.VARCHAR, ""));
        }else {
            para.put(i++, new DbParameter(Types.VARCHAR, JSONObject.toJSONString(data.getCurGame())));
        }
        para.put(i++, new DbParameter(Types.BIGINT, data.getTotalScore()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getSweetVal()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getTangHuLuNum()));
        if(data.getSeniorMakeData() == null){
            para.put(i++, new DbParameter(Types.VARCHAR, ""));
        }else {
            para.put(i++, new DbParameter(Types.VARCHAR, JSONObject.toJSONString(data.getSeniorMakeData())));
        }
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        if (getDb().executeNoneQuery(updateTangHuLuUserDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected TangHuLuUserData parseData(ResultSet rs) throws SQLException {
        TangHuLuUserData data = new TangHuLuUserData();
        data.setActivityId(rs.getInt("activityId"));
        data.setUserId(rs.getLong("userId"));
        String makeDataStr = rs.getString("makeData");
        if(!StringUtils.isNullOrEmpty(makeDataStr)){
            TangHuLuMakeData makeData = JSON.parseObject(makeDataStr, new TypeReference<TangHuLuMakeData>() {});
            if(makeData != null){
                data.setCurGame(makeData);
            }
        }
        data.setTotalScore(rs.getLong("totalScore"));
        data.setSweetVal(rs.getLong("sweetVal"));
        data.setTangHuLuNum(rs.getInt("tangHuLuNum"));
        makeDataStr = rs.getString("seniorMakeData");
        if(!StringUtils.isNullOrEmpty(makeDataStr)){
            TangHuLuMakeData makeData = JSON.parseObject(makeDataStr, new TypeReference<TangHuLuMakeData>() {});
            if(makeData != null){
                data.setSeniorMakeData(makeData);
            }
        }
        return data;
    }
}
